$(function () {

    $("#tou").load("tou.html");

    //分类列表
    categoryList(1, 8);

    //添加分类
    toAddCategory(1,8);


})

//分类列表
function categoryList(pageNum, pageSize) {
    $.ajax({
        url: "easybuy/allcategorylist",
        data: {
            pageNum: pageNum,
            pageSize: pageSize
        },
        success: function (pageInfo) {
            var categoryList = pageInfo.list;
            $(".order_tab").empty();
            $(".order_tab").append("" +
                "                <tr>\n" +
                "                    <td width=\"25%\">分类名称</td>\n" +
                "                    <td width=\"25%\">分类级别</td>\n" +
                "                    <td width=\"25%\">父级分类</td>\n" +
                "                    <td width=\"25%\">操作</td>\n" +
                "                </tr>");
            $.each(categoryList, function (index, category) {
                $(".order_tab").append("" +
                    "                <tr data-id=" + category.id + ">\n" +
                    "                    <td width=\"25%\">" + category.name + "</td>\n" +
                    "                    <td width=\"25%\">" + category.type + "级分类</td>\n" +
                    "                    <td width=\"25%\" data-id=" + category.parentid + ">" + category.parentname + "</td>\n" +
                    "                    <td width=\"25%\"><a href='javascript:;'class='modify'>修改</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "                                      <a href='javascript:;' class='del'>删除</a>" +
                    "                    </td>\n" +
                    "                </tr>");
            });
            pages(pageInfo, pageNum, pageSize);

            //删除
            deleteCategory(pageNum, pageSize);
            //修改
            mdfCategory(pageNum, pageSize);

        }
    });
}

//加载添加分类
function toAddCategory(pageNum, pageSize) {
    $("#toAddCategory").click(function () {
        $("#toAddCategory").hide();
        $("#selectLevel").prev().show();
        $("#selectLevel").next().show();


        var level = $("[name='level']").val();
        $("[name='level']").change(function () {

            $("#selectLevel").empty();
            level = $("[name='level']").val();

            //判断是否选择分类级别
            if (level==0){
                $(".level span").show();
            }else {
                $(".level span").hide();
            }


            if (level == 2) {
                $("#selectLevel").append(
                    "               <div id=\"one\">一级分类 ：\n" +
                    "                    <select name=\"one\"></select>\n" +
                    "                </div>");
                $.ajax({
                    url: "easybuy/procategorylist",
                    data: {
                        parentId: 0
                    },
                    success: function (categoryList) {
                        // $("[name='one']").append("<option value=\"\">-请选择-</option>");
                        $.each(categoryList, function (index, category) {
                            $("[name='one']").append("<option value=\"" + category.id + "\">" + category.name + "</option>");
                        });
                            submitAdd(level,pageNum, pageSize);
                    }
                })
            }
            if (level == 3){
                $("#selectLevel").append(
                    "               <div id=\"one\">一级分类 ：\n" +
                    "                    <select name=\"one\"></select><span>请选择一级分类</span>\n" +
                    "                </div>");
                $.ajax({
                    url: "easybuy/procategorylist",
                    data: {
                        parentId: 0
                    },
                    success: function (categoryList) {
                        $("[name='one']").append("<option value=\"-2\" disabled selected>-请选择-</option>");
                        $.each(categoryList, function (index, category) {
                            $("[name='one']").append("<option value=\"" + category.id + "\">" + category.name + "</option>");
                        });
                    }
                })
                $("#selectLevel").append(
                    "          <div  id=\"two\">二级分类 ：\n" +
                    "                    <select name=\"two\"></select><span></span>\n" +
                    "                </div>");
                $("[name='one']").change(function () {
                    $("#one>span").text("");
                    parentId = $("[name='one']").val();
                    $.ajax({
                        url: "easybuy/procategorylist",
                        data: {
                            parentId: parentId
                        },
                        success: function (categoryList) {
                            $("[name='two']").empty();
                            $.each(categoryList, function (index, category) {
                                $("[name='two']").append("<option value=\"" + category.id + "\">" + category.name + "</option>");
                            });
                            submitAdd(level,pageNum, pageSize);
                        }
                    })



                })
            }
            submitAdd(level,pageNum, pageSize);
        });
        if (level==0){
            $(".level span").show();
        }
    });
}


//添加提交
function submitAdd(level,pageNum, pageSize) {
    $("#Submit").unbind("click").click(function () {
        var newCategoryName = $("#newCategoryName").children().val();
        var parentId;
        if (level == "1") {
            parentId = 0;
        } else if (level == "2") {
            parentId = $("[name='one']").val();
        } else if (level == "3") {
            parentId = $("[name='two']").val();
        }
        //未输入新分类名称
        if (newCategoryName == null || newCategoryName == "" ||level=="") {
            $("#newCategoryName").find("span").empty();
            $("#newCategoryName").find("span").append("请输入分类名");
            return;
        }
        $.ajax({
            url: "easybuy/addcategory",
            data: {
                name: newCategoryName,
                parentid: parentId,
                type: level
            },
            success: function (result) {
                if (result=="exist"){
                    alert("该分类已存在，请输入新的分类名！");
                }
                if (result=="success") {
                    alert("添加成功");
                    location.href = "adminCategory.html";
                }

            }
        });
    });
}


//修改
function mdfCategory(pageNum, pageSize) {
    $(".modify").click(function () {
        var categoryId = $(this).parent().parent().data("id");
        var parentId = 0;
        // var parentsId = 0;


        $("#selectLevel").prev().show();
        $("#level").attr("disabled","disabled");
        $(".level h3").text("修改分类");
        $("#selectLevel").next().show();


        // var type = 0;
        $.ajax({
            url: "easybuy/categorybyid",
            data: {
                categoryId: categoryId
            },
            success: function (category) {
                $("[name='level']").val(category.type);
                $("#newCategoryName").children().val(category.name);
                type = category.type;
                parentId = category.parentid;
                $("#selectLevel").empty();
                $("#selectLevel").prepend(
                    "          <div  id=\"one\">父级分类 ：\n" +
                    "                    <select name=\"one\"></select>\n" +
                    "                </div>");
                $.ajax({
                    url: "easybuy/categorybytype",
                    data: {
                        type: (type-1)
                    },
                    success: function (categoryList) {
                        $.each(categoryList, function (index, category) {
                            if (category.id == parentId) {
                                $("[name='one']").append("<option value=\"" + category.id + "\" selected>" + category.name + "</option>");
                            } else {
                                $("[name='one']").append("<option value=\"" + category.id + "\">" + category.name + "</option>");
                            }

                        });
                    }
                });
                submitMdf(categoryId, type, pageNum, pageSize);

            }
        });
    });
}

//提交修改
function submitMdf(id, level, pageNum, pageSize) {
    $("#Submit").unbind("click").click(function () {
        var newCategoryName = $("#newCategoryName").children().val();
        var parentId=$("[name='one']").val();;
        //未输入新分类名称
        //未输入新分类名称
        if (newCategoryName == null || newCategoryName == "") {
            $("#newCategoryName").find("span").empty();
            $("#newCategoryName").find("span").append("请输入分类名");
            return;
        }
        $.ajax({
            url: "easybuy/mdfcategory",
            data: {
                id: id,
                name: newCategoryName,
                parentid: parentId,
                type: level
            },
            success: function (result) {
                if (result == "success") {
                    alert("修改成功");
                    categoryList(pageNum, pageSize);
                }
                $("#toAddCategory").show();
                $("#selectLevel").prev().hide();
                $(".level h3").removeAttr("disabled");
                $(".level h3").text("新增分类");
                $("#selectLevel").empty();
                $("#selectLevel").next().hide();
            }, error: function () {
                alert("修改失败！")
            }
        });
    });
}


//删除
function deleteCategory(pageNum, pageSize) {
    $(".del").click(function () {
        var categoryId = $(this).parent().parent().data("id");
        $.ajax({
            url: "easybuy/delcategory",
            data: {
                categoryId: categoryId
            },
            success: function (result) {
                if (result == "hasChildren") {
                    alert("该分类下还存在子分类，暂不可删除！");
                } else if (result == "hasProduct") {
                    alert("该分类下还存在商品，暂不可删除！");
                } else if (result) {
                    alert("删除成功");
                    categoryList(pageNum, pageSize);
                }
            }
        })
    });

}


//分页
function pages(pageInfo, pageNum, pageSize) {
    $(".pages").empty();


    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='first'>首页</a>");
    $("#first").click(function () {
        categoryList(1, pageSize);
    });
    //页数
    for (let i = 0; i < pageInfo.navigatepageNums.length; i++) {
        if (pageNum == i + 1) {
            $(".pages").append("<a href=\"javascript:;\" class=\"cur number\">" + (pageInfo.navigatepageNums[i]) + "</a>");
        } else {
            $(".pages").append("<a href=\"javascript:;\" class='number'>" + (pageInfo.navigatepageNums[i]) + "</a>");
        }
    }
    $(".number").click(function () {
        var pageNum = $(this).text();
        categoryList(pageNum, pageSize);
    })
    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='last'>末页</a>");
    $("#last").click(function () {
        categoryList(pageInfo.navigatepageNums.length, pageSize)
    })
}
